<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Lang;
class Article extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($cid=null)
    {   
        if ($cid) {
			$map = ['a.cid' => $cid];
		} else {
			$map = [];
		}
        $art=db('article')
        ->alias('a')
        ->join('category c','c.id=a.cid')
        ->join('pic p','p.aid=a.id','LEFT')
        ->field('a.*,c.cate_name,count(p.pic) as pic')
        ->group('a.id')
        ->where($map)
        ->order('a.istop DESC,a.toptime DESC,a.addtime DESC')
        ->paginate(10,false,['query' => ['cid' => $cid]]);
        $data=model('Category')->order('sort DESC,id ASC')->select();
        $cate=model('Category')->getCateAll($data,$pid=0,$level=-1);
        $this->assign(array(
            'art'=>$art,
            'cate'=>$cate,
            'cid'=>$cid,
        ));
        return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {   
        $data=model('Category')->select();
        $cate=model('Category')->getCateAll($data);
        $this->assign('cate',$cate);
        return view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if (request()->isPost()) {
            $data=$request->param();
            $data['addtime']=strtotime($data['addtime']);
            if (isset($data['istop'])) {
                $data['toptime']=time();
            }
            $validate=validate('Article');
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            $res=model('Article')->allowField(true)->save($data);
            Lang::set('Successful addition of articles','文章添加成功','zh-cn');
            Lang::set('Content addition failure', '内容添加失败', 'zh-cn');
            if (!$res) {
                $this->error('文章添加失败');
            }
            
            $this->success(lang('Successful addition of articles'),'index');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {   
        $article=db('article')
        ->alias('a')
        ->join('pic p','p.aid=a.id')
        ->field('a.*,GROUP_CONCAT(pic) as pic')
        ->where('a.id',$id)
        ->find();
        $article['pic']=explode(',',$article['pic']);
        $data=model('Category')->select();
        $cate=model('Category')->getCateAll($data);
        $this->assign(array(
            'cate'=>$cate,
            'article'=>$article,
        ));
        return view();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
       if (request()->isPost()) {
           $data=$request->param();
           $data['addtime'] = strtotime($data['addtime']);
			if (isset($data['istop'])) {
				$data['toptime'] = time();
			}
        $validate=validate('Article');
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }
        $res=model('Article')->allowField(true)->save($data,['id'=>$id]);
        if (!$res) {
            $this->error('文章编辑失败');
        }
        
        $this->success('文章编辑成功','index');
       }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (model('Article')->destroy($id)) {
           $this->success('删除成功','index');
        }
        $this->error('删除失败');
    }
     /**
     * 批量删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function deleteAll()
    {
        if (request()->isPost()) {
            $data=input('post.');
            if (empty($data)) {
                $this->error('请选择要删除的数据');
            }
            if (model('Article')->destroy($data['id'])) {
                $this->success('删除成功','index');
             }
             $this->error('删除失败');
        }
    }
     /**
     * 内容置顶
     *
     * @return \think\Response
     */
    public function roof()
    {
        if (request()->isAjax()) {
            $data=input('post.');
            $value=array();
            $value['id']=$data['aid'];
            $value['toptime']=time();
            if ($data['istop']==="true") {
                $value['istop']=1;
                $res=model('Article')->update($value);
                if ($res) {
                    return json(['code' => 1, 'msg' => "置顶成功"]);
                }
                return json(['code' => 0, 'msg' => "操作失败"]);
            }else{
                $value['istop']=0; 
                $res=model('Article')->update($value);
                if ($res) {
                    return json(['code' => 1, 'msg' => "取消置顶成功"]);
                }
                return json(['code' => 0, 'msg' => "操作失败"]);
            }

        }
    }
    /**
     * 图片显示
     */
    public function piclist($aid){
        $images=db('pic')->where('aid',$aid)->order('sort desc')->select();
        $this->assign('images',$images);
        return view();
    }
    /**
     * 内容图片删除
     */
    public function delpic($id){
        $res=db('pic')->find($id);
        $path=ROOT_PATH."\\public\\".$res['pic'];
        if (file_exists($path)) {
            @unlink($path);
        }
        if (db('pic')->delete($id)) {
            return json(['code'=>1,'msg'=>'删除成功']);
        }
        return json(['code'=>0,'msg'=>'删除失败']);
    }
    /**
     * 图片排序
     */
    public function picsort(){
        if (request()->isAjax()) {
            $data=input('post.');
            if (db('pic')->update(['id'=>$data['id'],'sort'=>$data['sort']])) {
                return json(['code'=>1,'msg'=>'排序成功']);
            }
            return json(['code'=>0,'msg'=>'排序失败']);
        }
    }

}
